<?php

namespace App\Admin\Controllers;

use App\Http\Requests\MembersRequest;
use App\Member;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;


class MembersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('注册用户列表')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Member);
        $grid->actions(function ($actions) {
          //  $actions->disableDelete();
         //   $actions->disableEdit();
            $actions->disableView();
        });
        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->equal('status', '核销状态')->radio([
                0 => '未核销',
                1 => '已核销',
            ]);

            $filter->like('phone', '手机号');

        });
        $grid->id('Id');
        $grid->column('openid', 'Openid');
        $grid->column('nickname', '微信昵称');
        $grid->column('head_image', '微信头像')->display(function ($v){
            if (!$v){
                return '';
            }
           return '<img src="'.$v.'" width="50%"/>';
        });
        $grid->name('名字');
        $grid->phone('手机号');
        $grid->address('地址');
        $grid->hospital('医院');
        $grid->section('科室');
        $grid->job('职称');
        $grid->created_at('注册时间');
        $states = [
            'on'  => ['value' => 1, 'text' => '已核销', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '未核销', 'color' => 'default'],
        ];
        $grid->column('status', '核销状态')->switch($states);
        $grid->column('accepted_at', '核销时间');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Member::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->openid('Openid');
        $show->phone('Phone');
        $show->address('Address');
        $show->hospital('Hospital');
        $show->section_id('Section id');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Member);

        $form->text('name', '名字');
        $form->mobile('phone', '电话');
        $form->text('address', '地址');
        $form->text('hospital', '所属医院');
        $form->text('section', '所属科室');
        $states = [
            'on'  => ['value' => 1, 'text' => '已核销', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '未核销', 'color' => 'default'],
        ];
        $form->switch('status', '核销')->states($states);
        return $form;
    }
}
